///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.utils {
	import sugarcookie.error.ApplicationError;
	
	public class NumberUtil {
		public static const GREATER_THAN:String = "greater_than";
		public static const LESS_THAN:String = "less_than";
		
		private static var avg:Array;
		
		public static function compareNumbers(val1:Number,val2:Number,compareType:String):int {
			var result:Number;
			
			if (compareType!=GREATER_THAN && compareType!=LESS_THAN) throw new ApplicationError("Not a valid compare type.");
			
			switch (compareType) {
				case GREATER_THAN:
					result = val1>val2?val1:val2;
					break;
				case LESS_THAN:
					result = val1<val2?val1:val2;
					break;
			}
			
			return result;
		}
		
		public static function range(low_value:Number,high_value:Number,current_value:Number):Boolean {
			if ((current_value>low_value && current_value<high_value) || current_value==low_value || current_value==high_value) return true;
			else return false;
		}
		
		public static function rollingAverage(newValue:Number,length:int=5):Number {
			if (!avg) avg = [];
			
			avg.unshift(newValue)
			avg.length = length;
			
			var len:int = avg.length;
			var total:Number = 0;
			for (var i:int=0;i<len;i++) total += avg[i];
			return total/len;
		}
	}
}